/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dal;

import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.CallableStatement;
import java.sql.Types;


/**
 *
 * @author Le Thai
 */
public class HocSinhDAL {
    DbAccess conn=new DbAccess();
    PreparedStatement pstm;
    Statement stm;
    ResultSet res=null;
    String query="";
    //Constructor
    public HocSinhDAL(){
        
    }
    
    //close connection
    public void Close(){
        conn.Close();
    }
    
    //find student
    public ResultSet timHocSinh(String ten,int maLop, int maNh){
        conn.getDefaultConnection();
        try {
            query="exec timHocSinh ?,?,?";
            pstm=conn.Connection().prepareCall(query);
            pstm.setString(1,ten);
            pstm.setInt(2,maLop);
            pstm.setInt(3, maNh);
            res=pstm.executeQuery();
        } catch (Exception e) {
            System.out.println("[HocSinhDAL][timHocSinh]"+e.getMessage());
        }
        return res;
    }
    //tim theo ma hoc sinh
    public ResultSet timTheoMaHs(int maHs,int maNh){
        conn.getDefaultConnection();
        query="exec timHsTheoMaHs ?,?";
        try {
            pstm=conn.Connection().prepareCall(query);
            pstm.setInt(1, maHs);
            pstm.setInt(2, maNh);
            res= pstm.executeQuery();
        } catch (Exception e) {
            System.out.println("[HocSinhDAL][timTheoMaHs]" + e.getMessage());
        }
        return res;
    }
        

    //them hs moi
    public int themHocSinh(String hodem,String ten, boolean gt,String ns, String dc, int malop){
        CallableStatement cstm;
        conn.getDefaultConnection();
        int id;
        try {
            query="exec themHocSinh ?,?,?,?,?,?,?";
            cstm=conn.Connection().prepareCall(query);
            cstm.registerOutParameter(7,Types.INTEGER);
            cstm.setString(1,hodem);
            cstm.setString(2,ten);
            cstm.setBoolean(3,gt);
            cstm.setString(4,ns);
            cstm.setString(5,dc);
            cstm.setInt(6,malop);
            cstm.execute();
            id=cstm.getInt(7);
            System.out.println(""+id);
            
            
        } catch (Exception e) {
            System.out.println("[HocSinhDAL][themHocSinh]"+e.getMessage());
            id=0;
        }
        return id;
    }
    
    //xoa hs
    public boolean xoaHocSinh(int maHs){
        boolean isSuccess;
        conn.getDefaultConnection();
        query="exec xoaHocSinh ?";
        try {
            pstm=conn.Connection().prepareCall(query);
            pstm.setInt(1, maHs);
            pstm.executeUpdate();
            isSuccess=true;
        } catch (Exception e) {
            System.out.println("[HocSinhDAL][xoaHocSinh]" + e.getMessage());
            isSuccess=false;
        }
        return isSuccess;
    }
        
    //cap nhat
    public boolean capNhatHocSinh(int maHs,String hodem,String ten, boolean gt,
                                  String ns, String dc, int malop){
        conn.getDefaultConnection();
        boolean isUpdate;
        try {
            query="exec capNhatHocSinh ?,?,?,?,?,?,?";
            pstm=conn.Connection().prepareCall(query);
            pstm.setInt(1,maHs);
            pstm.setString(2,hodem);
            pstm.setString(3,ten);
            pstm.setBoolean(4,gt);
            pstm.setString(5,ns);
            pstm.setString(6,dc);
            pstm.setInt(7,malop);                  
            pstm.executeUpdate();
            isUpdate=true;
        } catch (Exception e) {
            System.out.println("[HocSinhDAL][capNhatHocSinh]"+e.getMessage());
            isUpdate=false;
        }
        return isUpdate;
    }
}
